# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=dropbear
pkgver=2024.84
pkgrel=2
pkgdesc="small SSH 2 client/server designed for small memory environments"
url="https://matt.ucc.asn.au/dropbear/dropbear.html"
arch="all"
license="MIT"
options="!check"
makedepends="zlib-dev linux-headers utmps-dev utmps-static autoconf automake"
subpackages="$pkgname-doc
	$pkgname-openrc
	$pkgname-convert:_convert
	$pkgname-dbclient:_dbclient
	$pkgname-ssh:_ssh:noarch
	$pkgname-scp:_scp"
source="https://matt.ucc.asn.au/dropbear/releases/dropbear-$pkgver.tar.bz2
	dropbear.initd
	dropbear.confd
	dropbear-options_sftp-server_path.patch
	dropbear-fix-utmp.patch
	new-config.h-location.patch
	"

# secfixes:
#   2022.83-r4:
#     - CVE-2023-48795
#   2020.79-r0:
#     - CVE-2018-20685
#   2018.76-r2:
#     - CVE-2018-15599

_progs="dropbear dropbearkey dbclient dropbearconvert scp"

prepare() {
	default_prepare
	autoreconf -fvi
}

build() {
	CFLAGS="$CFLAGS $(pkg-config --cflags --static utmps)" \
	LIBS="$LDLIBS $(pkg-config --libs --static utmps)" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--disable-utmp \
		--disable-wtmp \
		--disable-pututline \
		--disable-lastlog
	make PROGRAMS="$_progs"
}

package() {
	make PROGRAMS="$_progs" DESTDIR="$pkgdir" install

	install -dm755 "$pkgdir"/etc/dropbear
	install -D -m755 "$srcdir"/dropbear.initd \
		"$pkgdir"/etc/init.d/dropbear
	install -D -m644 "$srcdir"/dropbear.confd \
		"$pkgdir"/etc/conf.d/dropbear
}

_convert() {
	pkgdesc="$pkgname dropbearconvert command"
	depends=""

	amove usr/bin/dropbearconvert
}

_dbclient() {
	pkgdesc="$pkgname dbclient command"
	depends=""

	amove usr/bin/dbclient
}

_scp() {
	pkgdesc="$pkgname scp support"
	depends="!openssh-client"

	amove usr/bin/scp
}

_ssh() {
	pkgdesc="$pkgname ssh command (compatibility symlink for dbclient)"
	depends="$pkgname-dbclient !openssh-client"

	mkdir -p "$subpkgdir"/usr/bin
	ln -s dbclient "$subpkgdir"/usr/bin/ssh
}

sha512sums="
254daea819c5aeaa65bb43449386fb964f4aa13e3b3037fe11064120205c6e265925e7ef2d84f7ebe66c6a00cf0a22e6010314c065ed49a3815f47137b7aca44  dropbear-2024.84.tar.bz2
55af2b9ece9845f30e07f707474c81d78921b17abfe614669fcefa600868d4ecef007a1e2276b74db8675ba5562c78397da6ae8de8717a2ca9c425384dc519d5  dropbear.initd
83f2c1eaf7687917a4b2bae7d599d4378c4bd64f9126ba42fc5d235f2b3c9a474d1b3168d70ed64bb4101cc251d30bc9ae20604da9b5d819fcd635ee4d0ebb0f  dropbear.confd
09e73041ecb55d5d97106b3b8f4256df9e6d84c7aceeaaccdb148a9243ccd4defaccb320c45bf76620f5b004264dd775dbfc207db4a045ee1c7b7db2dedb2d79  dropbear-options_sftp-server_path.patch
5c0d118944481237611cdbe9239bd26548859dc2ab04f2e81854388de9c34c99e083843e925312b4f90ce8b22037bef3c61858fc19d843e5b6db5f3006448304  dropbear-fix-utmp.patch
b773b90e92f451a99b6f0e98c85b7fd614ed9a3c1562e086c4e2db1accd1cfec1129b24f3e0731411c3f09014902bc6e9493ebbc2fafc2438e51cfc450e25f15  new-config.h-location.patch
"
